package com.zjc.yohoshop.mvp.persenter;

import com.zjc.lib_core.mvp.presenter.BasePresenter;
import com.zjc.yohoshop.bean.AddAddressEntity;
import com.zjc.yohoshop.bean.AddressListEntity;
import com.zjc.yohoshop.bean.DelAddressEntity;
import com.zjc.yohoshop.mvp.contract.AddressListFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AddressListFragmentPersenter extends BasePresenter<AddressListFragmentContract.AddressListFragmentIView, AddressListFragmentContract.AddressListFragmentIModel> {
    @Inject
    public AddressListFragmentPersenter(AddressListFragmentContract.AddressListFragmentIView addressListFragmentIView, AddressListFragmentContract.AddressListFragmentIModel addressListFragmentIModel) {
        super(addressListFragmentIView, addressListFragmentIModel);
    }

    /**
     * 地址列表
     * @param user_id
     */
    public void addressListRequest(String user_id){

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("user_id", user_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        m.addressListRequest(jsonObject.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddressListEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(AddressListEntity addressListEntity) {
                        v.hideDialog();
                        v.addressListSuccess(addressListEntity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    /**
     * 添加地址
     */
    public void addAddressRequest(String user_id, String user_name, String phone, String address_area, String address_detail, String address_tag){

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("user_id", user_id);
            jsonObject.put("user_name", user_name);
            jsonObject.put("phone", phone);
            jsonObject.put("address_area", address_area);
            jsonObject.put("address_detail", address_detail);
            jsonObject.put("address_tag", address_tag);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        m.addAddressRequest(jsonObject.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddAddressEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(AddAddressEntity entity) {
                        v.hideDialog();
                        v.addAddressSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });

    }

    /**
     * 删除地址
     */
    public void delAddressRequest(String address_id){

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("address_id", address_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        m.delAddressRequest(jsonObject.toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DelAddressEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(DelAddressEntity entity) {
                        v.hideDialog();
                        v.delAddressSuccess(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });

    }


}
